package com.ttedu.action.manage.items;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.ttedu.base.BaseAction;
import com.ttedu.database.model.items;
import com.ttedu.database.model.itemsanswers;
import com.ttedu.database.service.ItemsService;
import com.ttedu.util.KeyUtil;
import com.ttedu.util.StringUtil;
import com.ttedu.util.Validator;

public class ItemsAddAction extends BaseAction{
	private String bankstree;
	private String chaptertipstree;
	private items item=new items();
	@Autowired
	private ItemsService itemsservice;
	//跳转到添加第一页
	@Action(value="/cas/items_addfirst",results={ @Result(name="success",location="/WEB-INF/html/manage/items/addfirst.jsp")})
	public String items_addfirst(){
		return "success";
	}
	
	@Action(value="/cas/items_addsecondhtml",results={ @Result(name="success",location="/WEB-INF/html/manage/items/addsecond.jsp")})
	public String items_addsecondhtml(){
		bankstree=request.getParameter("bankstree");
		chaptertipstree=request.getParameter("chaptertipstree");
		request.setAttribute("bankstree", bankstree);
		request.setAttribute("chaptertipstree", chaptertipstree);
		return "success";
	}
	//跳转到添加第二页传参
	@Action(value = "/cas/items_addsecond", results = { @Result(name = "success",type="redirect", location = "/cas/items_addsecondhtml.action?bankstree=${bankstree}&chaptertipstree=${chaptertipstree}")})
	public String items_addsecond() {
		bankstree=request.getParameter("bankstree");
		chaptertipstree=request.getParameter("chaptertipstree");
		return "success";
	}
	//保存或更新
	@Action(value = "/cas/items_updateorsave", results = { @Result(name = "success",type="redirect", location = "/cas/items_addsecondhtml.action?bankstree=${bankstree}&chaptertipstree=${chaptertipstree}")})
	public String items_updateorsave() {
		bankstree=request.getParameter("bankstree");//题库
		chaptertipstree=request.getParameter("chaptertipstree");//知识点
		String radioname=request.getParameter("radioname");//单选框
		String[] checkboxname=request.getParameterValues("checkboxname");//复选框
		String dtype=request.getParameter("dtype");//原始题型
		String answers=request.getParameter("answers");//选项个数
		int intdtype=Integer.valueOf(dtype);
		List<itemsanswers> listitemsanswers=new ArrayList<itemsanswers>();
		//单选题
		if(intdtype==KeyUtil.DTYPES_RADIO){
			int intanswers=Integer.valueOf(answers);
			for(int i=1;i<=intanswers;i++){
				itemsanswers answer=new itemsanswers();
				String content=request.getParameter("textareaname"+i);//答案
				answer.setContent(content);
				int intradioname=Integer.valueOf(radioname);
				//答案是否正确
				if(intradioname==i){
					//为正解答案
					answer.setCorrect(1);
				}else{
					answer.setCorrect(0);
				}
				listitemsanswers.add(answer);
			}
		}
		//复选题
		else if(intdtype==KeyUtil.DTYPES_CHECKBOX){
			int intanswers=Integer.valueOf(answers);
			for(int i=1;i<=intanswers;i++){
				itemsanswers answer=new itemsanswers();
				String content=request.getParameter("textareaname"+i);//答案
				answer.setContent(content);
				if(StringUtil.isHave(checkboxname, i+"")){
					//为正解答案
					answer.setCorrect(1);
				}else{
					answer.setCorrect(0);
				}
				listitemsanswers.add(answer);
			}
		}
		//单行文本
		else if(intdtype==KeyUtil.DTYPES_INPUT){
			itemsanswers answer=new itemsanswers();
			String content=request.getParameter("textareaname");//答案
			answer.setContent(content);
			answer.setCorrect(1);
		}
		//多行文本
		else if(intdtype==KeyUtil.DTYPES_TEXTARER){
			itemsanswers answer=new itemsanswers();
			String content=request.getParameter("textareaname");//答案
			answer.setContent(content);
			answer.setCorrect(1);
		}
		
		//第一步，保存题目
		items itemone=new items();
		itemone.setContent(item.getContent());//题目
		itemone.setScore(item.getScore());//分数
		itemone.setStatus(item.getStatus());//状态
		itemone.setCreatedAt(new Date());
		itemone.setUpdatedAt(new Date());
		itemone.setTypeId(item.getTypeId());//题型
		itemone.setKeyword(item.getKeyword());//关键字
		itemone.setDirection(item.getDirection());//说明
		itemone.setAnswerprocess(item.getAnswerprocess());//解答过程
		itemsservice.saveItems(itemone, listitemsanswers, bankstree, chaptertipstree);
		return "success";
	}
	public String getBankstree() {
		return bankstree;
	}
	public void setBankstree(String bankstree) {
		this.bankstree = bankstree;
	}
	public String getChaptertipstree() {
		return chaptertipstree;
	}
	public void setChaptertipstree(String chaptertipstree) {
		this.chaptertipstree = chaptertipstree;
	}

	public items getItem() {
		return item;
	}

	public void setItem(items item) {
		this.item = item;
	}
	
}
